
#include "eye_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"

#define CORENUM 40


namespace optiling {
    static ge::graphStatus TilingFunc(gert::TilingContext* context){

        EyeTilingData tiling;
        uint32_t sizeofdatatype;
        uint32_t totalLengthAligned;

        // 1. 获取平台信息
        // uint64_t ub_size;
        // auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
        // ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);

        // 2. 获取数据信息
        uint32_t totalLength = context->GetInputTensor(0)->GetShapeSize();


        const int64_t* pnum_rows = context->GetAttrs()->GetInt(0);
        const int64_t* pnum_columns = context->GetAttrs()->GetInt(1);

        uint16_t rows = *pnum_rows;
        uint16_t cols = *pnum_columns;

        int32_t batchSize = rows * cols;
        int32_t batchNum = totalLength / batchSize;

        tiling.set_rows(rows);
        tiling.set_cols(cols);

        tiling.set_batchNum(batchNum);

        uint32_t CoreNum = (batchNum < CORENUM) ? batchNum : CORENUM;

        context->SetBlockDim(CoreNum);

        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(),
                            context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
        size_t* currentWorkspace = context->GetWorkspaceSizes(1);
        currentWorkspace[0] = 0;
        return ge::GRAPH_SUCCESS;
    }
}


// namespace ge {
// static ge::graphStatus InferShape(gert::InferShapeContext* context)
// {
//     const gert::Shape* x1_shape = context->GetInputShape(0);
//     gert::Shape* y_shape = context->GetOutputShape(0);
//     *y_shape = *x1_shape;
//     return GRAPH_SUCCESS;
// }
// }


namespace ops {
class Eye : public OpDef {
public:
    explicit Eye(const char* name) : OpDef(name)
    {
        this->Input("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_DOUBLE, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_DOUBLE, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("num_rows").Int();
        this->Attr("num_columns").AttrType(OPTIONAL).Int(0);
        this->Attr("batch_shape").AttrType(OPTIONAL).ListInt({});
        this->Attr("dtype").AttrType(OPTIONAL).Int(0);

        // this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");

    }
};

OP_ADD(Eye);
}
